package com.gxa.worker.recycling.service.impl;

import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.Recycleorder;
import com.gxa.core.recycling.pojo.Recycler;
import com.gxa.worker.recycling.mapper.RecycleitemMapper;
import com.gxa.worker.recycling.mapper.RecycleorderMapper;
import com.gxa.worker.recycling.mapper.RecyclerMapper;
import com.gxa.core.recycling.pojo.*;
import com.gxa.worker.recycling.mapper.*;
import com.gxa.worker.recycling.service.RecycleorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author Bei
 * @date 2021/12/2 16:52
 */
@Service
@Transactional(rollbackFor = {Throwable.class})
public class RecycleorderServiceImpl implements RecycleorderService {
    @Autowired
    RecycleorderMapper recycleorderMapper;

    @Autowired
    RecycleitemMapper recycleitemMapper;

    @Autowired
    ServiceAddressMapper serviceAddressMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ScoreMapper scoreMapper;


    @Override
    public List<Map> selectByWarehoused(Long rid) {
        Long hid = recycleorderMapper.villAgeId(rid);
        String villAgeName = recycleorderMapper.findVillAgeName(hid);
        List<Map> maps = recycleorderMapper.selectByWarehoused(rid);
        for (int i = 0; i < maps.size(); i++) {
            maps.get(i).put("village",villAgeName);
        }
        return maps;
    }

    @Override
    public Map<String, Object> getOrderDeepInfoById(Long id) {
        HashMap<String, Object> map = new HashMap<>(16);
        //查询出回收预约单的单信息
        Map<String, Object> orderInfoById = recycleorderMapper.getOrderInfoById(id);
        map.put("orderInfo",orderInfoById);
        //订单项集合
        List<Map<String, Object>> itemByOrderId = recycleitemMapper.getItemByOrderId(id);
        map.put("item",itemByOrderId);
        return map;
    }

    @Override
    public void OrderReceiving(Recycleorder recycleorder) throws BusinessException {
        recycleorder.setState("待回收");
        int i = recycleorderMapper.updateByPrimaryKeySelective(recycleorder);
        if (i != 1){
            throw new BusinessException(ExceptionCode.ALTER_WRONG);
        }
    }

    @Override
    public void CancellationOrder(Recycleorder recycleorder) throws BusinessException {

        int i = recycleorderMapper.updateByPrimaryKeySelective(recycleorder);
        if (i != 1){
            throw new BusinessException(ExceptionCode.ALTER_WRONG);
        }
    }

    @Override
    public String CallCustomer(String name) {
        String phone = recycleorderMapper.callCustomer(name);
        return phone;
    }

    @Override
    public Long villAgeId(Long rid) {
        Long aLong = recycleorderMapper.villAgeId(rid);
        return aLong;
    }

    @Override
    public Integer countNewOrder(Long hid) {
        List<Map> maps = recycleorderMapper.selectNewOrder(hid);
        return maps.size();
    }

    @Override
    public Integer countIngOrder(Long hid) {
        List<Map> maps = recycleorderMapper.selectIngOreder(hid);
        return maps.size();
    }




    @Override
    public Integer countYiRuOrder(Long hid) {
        List<Map> maps = recycleorderMapper.selectYiRuOrder(hid);
        return maps.size();
    }

    @Override
    public void problemFeedback(List list, String content,Long id) {
//        String[] img1 = new String[img.length+3];
//        if (img.length < 3){
//            for (int i = img.length;i < 3;i++){
//                img1 = {img}
//            }
//        }
        recycleorderMapper.problemFeedback(list,content,id);
    }


    @Override
    public List<Map> selectNewOrder(Long rid) {
        Long hid = recycleorderMapper.villAgeId(rid);
            String villAgeName = recycleorderMapper.findVillAgeName(hid);
        String provinces = recycleorderMapper.findProvinces(hid);
        List<Map> maps = recycleorderMapper.selectNewOrder(hid);
        for (int i = 0; i < maps.size(); i++) {
            maps.get(i).put("village",villAgeName);
            maps.get(i).put("provinces",provinces);
        }
        return maps;
    }

    @Override
    public List<Map> selectIngOreder(Long rid) {
        Long hid = recycleorderMapper.villAgeId(rid);
        String villAgeName = recycleorderMapper.findVillAgeName(hid);
        List<Map> maps = recycleorderMapper.selectIngOreder(hid);
        String provinces = recycleorderMapper.findProvinces(hid);
        for (int i = 0; i < maps.size(); i++) {
            maps.get(i).put("village",villAgeName);
            maps.get(i).put("provinces",provinces);
        }
        return maps;


    }

    @Override
    public List<Map> selectOverOrder(Long rid) {
        Long hid = recycleorderMapper.villAgeId(rid);
        String villAgeName = recycleorderMapper.findVillAgeName(hid);
        List<Map> maps = recycleorderMapper.selectOverOrder(hid);
        String provinces = recycleorderMapper.findProvinces(hid);
        for (int i = 0; i < maps.size(); i++) {
            maps.get(i).put("village",villAgeName);
            maps.get(i).put("provinces",provinces);
        }
        return maps;


    }

    @Override
    public List<Map> selectCancelOrder(Long rid) {
        Long hid = recycleorderMapper.villAgeId(rid);
        String villAgeName = recycleorderMapper.findVillAgeName(hid);
        String provinces = recycleorderMapper.findProvinces(hid);
        List<Map> maps = recycleorderMapper.selectCancelOrder(hid);
        for (int i = 0; i < maps.size(); i++) {
            maps.get(i).put("village",villAgeName);
            maps.get(i).put("provinces",provinces);
        }
        return maps;
    }



    @Override
    public void DoorToDoorWeighing(Recycleorder recycleorder) throws BusinessException {
        List<Recycleorder> select = recycleorderMapper.select(recycleorder);
        if (select.size() == 0){
            throw new BusinessException(ExceptionCode.QUJIANCODE_WRONG);
        }
    }
    @Override
    public void finishOrder(Long orderId, Long userId, Long recyclerId,List<WasteItemVo> item) throws BusinessException {
        //大订单完善
        //取出用户
        User user = null;
        user = userMapper.selectByPrimaryKey(userId);
        Recycleorder recycleorder = null;
        if(orderId!=null){
            recycleorder = recycleorderMapper.selectByPrimaryKey(orderId);
            user = userMapper.selectByPrimaryKey(recycleorder.getUid());
        }

        StringBuffer buffer = new StringBuffer("");
        int allPoints = 0;
        for (int i = 0; i < item.size(); i++) {
            String title = item.get(i).getTitle();
            if (!(buffer.toString().contains(title))) {
                buffer.append(title);//System.out.println(i+1+"---"+title);
            }
            allPoints+=item.get(i).getPoints();
        }
        if(orderId==null){
            //临创用户，新建订单
            recycleorder = new Recycleorder();
            Example example = new Example(ServiceAddress.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("uid",userId);
            List<ServiceAddress> serviceAddresses = serviceAddressMapper.selectByExample(example);
            //存在
            ServiceAddress nowAddress = null;
            if (serviceAddresses.size()>0) {
                nowAddress = serviceAddresses.get(0);
            }else {
                //不存在地址就新建服务地址
                nowAddress = new ServiceAddress();
                nowAddress.setUid(userId);
                nowAddress.setCommnuityId(user.getQuartersId());
                nowAddress.setDaddress(user.getLocation());
                serviceAddressMapper.insert(nowAddress);
            }
            recycleorder.setDid(nowAddress.getId());
            recycleorder.setOrdertime(new Date());
            recycleorder.setUid(userId);
            recycleorder.setYtime(new Date());
            recycleorder.setZid(null);
            //回收标签
            recycleorder.setTitle(buffer.toString());

        }
        recycleorder.setHid(recyclerId);
        recycleorder.setRecoverytime(new Date());
        recycleorder.setState("待入库");
        recycleorder.setCode((int)(Math.random()*9000)+1000+"");
        if(orderId==null){
            recycleorderMapper.insertSelective(recycleorder);
        }else {
            recycleorderMapper.updateByPrimaryKey(recycleorder);
        }
        //订单项完善
        for (int i = 0; i < item.size(); i++) {
            WasteItemVo wasteItemVo = item.get(i);
            Recycleitem recycleitem = new Recycleitem();
            recycleitem.setYid(recycleorder.getId());
            recycleitem.setFid(wasteItemVo.getId());
            recycleitem.setWeight(wasteItemVo.getCount());
            recycleitemMapper.insertSelective(recycleitem);
        }
        //用户积分新增
        user.setNowPoints(user.getNowPoints()+allPoints);
        user.setAllPoints(user.getAllPoints()+allPoints);
        userMapper.updateByPrimaryKeySelective(user);

        //积分明细表
        Score score = new Score();
        score.setUid(user.getId());
        score.setStatus(true);
        score.setSerson("回收"+recycleorder.getTitle());
        score.setChangetime(recycleorder.getRecoverytime());
        score.setNum(allPoints);
        scoreMapper.insertSelective(score);

    }

    @Override
    public String findVillAgeName(Long id) {
        String villAgeName = recycleorderMapper.findVillAgeName(id);
        return null;
    }

    @Override
    public Map<String, Object> imgAndAddress(Long id) {
        Map<String, Object> map = recycleorderMapper.imgAndAddress(id);
        return map;
    }


}
